#!/usr/bin/bash
#
# ====================================================================================
# DOC: [Tone离线部署](https://openanolis.cn/sig/t-one/doc/1175048244250656802?lang=zh)
# ====================================================================================
#
# Install docker-compose on x86_64
#
# 1. Download `docker-compose-linux-$(uname -m)` from https://github.com/docker/compose/releases/tag/v2.30.3
#    e.g. wget https://github.com/docker/compose/releases/download/v2.30.3/docker-compose-linux-x86_64
#         wget https://github.com/docker/compose/releases/download/v2.30.3/docker-compose-linux-x86_64.sha256
# 2. Check `docker-compose-linux-$(uname -m)` is valid
#    e.g. code=$(sha256sum docker-compose-linux-x86_64 | awk '{print $1}')
#         grep "$code" docker-compose-linux-x86_64.sha256
#         (( $? == 0 )) && echo "OKAY" || echo "Oops, invalid docker-compose-linux-x86_64"
# 3. Copy `docker-compose-linux-$(uname -m)` to the target host
#    e.g. scp docker-compose-linux-x86_64 192.168.1.64:/var/tmp
#

function is_root
{
    [[ $(id -un) == "root" ]] && return 0 || return 1
}

is_root
(( $? != 0 )) && echo "Must be root." && exit 1

mkdir -p /usr/libexec/docker/cli-plugins
cp /var/tmp/docker-compose-linux-x86_64 /usr/libexec/docker/cli-plugins
cd /usr/libexec/docker/cli-plugins
ln -s docker-compose-linux-x86_64 docker-compose-linux
ln -s /usr/libexec/docker/cli-plugins/docker-compose /usr/local/sbin/docker-compose
